package web;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Dealer;
import model.Game;
import model.Player;
import model.Pot;
import model.Round;


@WebServlet("/CallActionServlet")
public class CallActionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public CallActionServlet() {
        super();
        // TODO Auto-generated constructor stub
    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ServletContext sc = getServletContext();
		Round round = (Round) sc.getAttribute("round");
		Dealer dealer = (Dealer) sc.getAttribute("dealer");
		@SuppressWarnings("unchecked")
		ArrayList<Player> playingList = (ArrayList<Player>) sc.getAttribute("playingList");
		Pot pot = (Pot) sc.getAttribute("pot");
		
		for(Player player : playingList){
			if(player.getSessionId()==request.getSession().getId()&&!player.hasFolded()){	//if your session is the one who clicked the action
				pot.setAmount(pot.getAmount() + (round.getLastBet() - player.getBet()));
				round.setLastBet(player.call(round.getLastBet()));
			}
		}
		round.determineNextToAct(dealer);
	}

}
